﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using Microsoft.AspNet.Identity;
using NPOI.HSSF.UserModel;
using OfficeOpenXml;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Threading.Tasks;


namespace GPS.Controllers.GPS_DanhMuc
{
    public class GPS_HangDT_MonHocController : Controller
    {

        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        public GPS.MODELS.ApplicationUser getCurrentUser()
        {
            ApplicationDbContext dbUser = new ApplicationDbContext();
            string userId = User.Identity.GetUserId();
            ApplicationUser _currentUser = dbUser.Users.Find(userId);
            return _currentUser;
        }

        #region Index
        public ActionResult Index(ViewModelSHANGDT_MONHOC SearchModel, int? currentPage)
        {
            initialCategorySearchAction(SearchModel);
            var entities = _db.Filter<SHANGDT_MONHOC>(o => (SearchModel.pDTMONHOC_MA == null || o.DTMONHOC_MA.StartsWith(SearchModel.pDTMONHOC_MA))
                                        && (SearchModel.pDTMONHOC_TEN == null || o.DTMONHOC_TEN.StartsWith(SearchModel.pDTMONHOC_TEN))
                                        && (SearchModel.pDTMONHOC_HANGDT_MA == null || o.DTMONHOC_HANGDT_MA.StartsWith(SearchModel.pDTMONHOC_HANGDT_MA))
                                        && (SearchModel.pDTMONHOC_ACTIVE == null || o.DTMONHOC_ACTIVE == SearchModel.pDTMONHOC_ACTIVE));
            ViewBag.TotalRow = entities.Count();
            var model = new List<SHANGDT_MONHOC>();
            Int16 icount = 1;
            foreach (var currentEntity in entities)
            {
                currentEntity.STT = icount.ToString();
                model.Add(Mapper.Map<SHANGDT_MONHOC>(currentEntity));
                icount++;
            }
            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
            return View(SearchModel);
        }
        #endregion

        //
        // Create: 
        #region create
        public ActionResult Create()
        {
            initialCategoryCreateAction();
            return View(new CrudModelSHANGDT_MONHOC());
        }

        [HttpPost]
        public ActionResult Create(CrudModelSHANGDT_MONHOC model)
        {
            initialCategoryCreateAction();
            if (ModelState.IsValid)
            {
                SHANGDT_MONHOC _model = new SHANGDT_MONHOC();
                GPS.COMMON.Helpers.CopyObject<SHANGDT_MONHOC>(model, ref _model);
                _db.Insert<SHANGDT_MONHOC>(_model);
                return RedirectToAction("Index");
            }
            return View(model);
        }
        public void initialCategoryCreateAction()
        {
            ViewBag.lstHangDT = new SelectList(_db.Filter<SHANGDT>(x => x.HANGDT_ACTIVE == "1"), "HANGDT_MA", "HANGDT_TEN");
            ViewBag.lstMonHoc = new SelectList(_db.Filter<SMONHOC>(x => x.MONHOC_ACTIVE == "1"), "MONHOC_MA", "MONHOC_TEN");
        }
        #endregion

        //
        // Edit: 
        #region Edit
        public ActionResult Edit(string id, string id1)
        {
            CrudModelSHANGDT_MONHOC _model = new CrudModelSHANGDT_MONHOC();
            SHANGDT_MONHOC model = _db.GetOne<SHANGDT_MONHOC>(o => o.DTMONHOC_MA == id && o.DTMONHOC_HANGDT_MA == id1);
            GPS.COMMON.Helpers.CopyObject<CrudModelSHANGDT_MONHOC>(model, ref _model);
            initialCategoryEditAction(_model);
            return PartialView("Edit", _model);
        }
        [HttpPost]
        public ActionResult Edit(CrudModelSHANGDT_MONHOC model, FormCollection form)
        {
            var _model = _db.GetOne<SHANGDT_MONHOC>(o => o.DTMONHOC_MA == model.DTMONHOC_MA && o.DTMONHOC_HANGDT_MA == model.DTMONHOC_HANGDT_MA);
            if (ModelState.IsValid)
            {
                GPS.COMMON.Helpers.CopyObject<SHANGDT_MONHOC>(model, ref _model);
                _db.Update<SHANGDT_MONHOC>(_model);
                string url = Url.Action("Index", "GPS_HangDT_MonHoc");
                return Json(new { success = true, url = url });
            }
            else
            {
                return PartialView("Edit", _model);
            }
            
        }
        #endregion

        #region Detele
        public ActionResult Delete(string id, string id1)
        {
            string _DTMONHOC_MA = id;
            string _DTMONHOC_HANGDT_MA = id1;
            CrudModelSHANGDT_MONHOC _model = new CrudModelSHANGDT_MONHOC();
            SHANGDT_MONHOC model = _db.GetOne<SHANGDT_MONHOC>(o => o.DTMONHOC_MA == _DTMONHOC_MA && o.DTMONHOC_HANGDT_MA == _DTMONHOC_HANGDT_MA);
            _db.DeleteItem<SHANGDT_MONHOC>(model);
            return RedirectToAction("Index");
        }
        #endregion

        #region Detail
        public ActionResult Detail(string id, string id1)
        {
            string _DTMONHOC_MA = id;
            string _DTMONHOC_HANGDT_MA = id1;
            CrudModelSHANGDT_MONHOC _model = new CrudModelSHANGDT_MONHOC();
            SHANGDT_MONHOC model = _db.GetOne<SHANGDT_MONHOC>(o => o.DTMONHOC_MA == _DTMONHOC_MA && o.DTMONHOC_HANGDT_MA == _DTMONHOC_HANGDT_MA);
            GPS.COMMON.Helpers.CopyObject<CrudModelSHANGDT_MONHOC>(model, ref _model);
            initialCategoryDetailAction(_model);
            return PartialView("Detail", _model);
        }
        #endregion

        public void initialCategorySearchAction(ViewModelSHANGDT_MONHOC viewModel)
        {
            ViewBag.lstHangDT = new SelectList(_db.Filter<SHANGDT>(x => x.HANGDT_ACTIVE == "1"), "HANGDT_MA", "HANGDT_TEN");
            string DVGTVT_MA = this.getCurrentUser().MA_TINHTP;
            ViewBag.checkuser = DVGTVT_MA;
        }

        public void initialCategoryDetailAction(CrudModelSHANGDT_MONHOC view)
        {
            view.DTMONHOC_ACTIVE = view.DTMONHOC_ACTIVE == "1" ? "Hiệu lực" : "Không hiệu lực";
        }
        public void initialCategoryEditAction(CrudModelSHANGDT_MONHOC view)
        {
            ViewBag.lstHangDT = new SelectList(_db.Filter<SHANGDT>(x => x.HANGDT_ACTIVE == "1"), "HANGDT_MA", "HANGDT_TEN");
            ViewBag.lstMonHoc = new SelectList(_db.Filter<SMONHOC>(x => x.MONHOC_ACTIVE == "1"), "MONHOC_MA", "MONHOC_TEN");
        }
    }
}